package com.example.qyweixin.utils;

import com.example.qyweixin.constants.Constants;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * HttpClient工具类
 */
public class HttpClientUtil {

    /**
     * 处理get请求
     * @param url
     * @return
     */
    public static String getResponse(String url){

        //建立客户端对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建请求HttpRequest对象
        HttpGet get= new HttpGet(url);

        try{
            //发送请求
            CloseableHttpResponse res = client.execute(get);
            //处理响应
            if (200 == res.getCode()) {
                String result = EntityUtils.toString(res.getEntity());

                return result;
            }
        }catch (IOException e){
            Logger.getLogger("请求处理失败",e.toString());
        }catch (ParseException e){
            Logger.getLogger("响应解析失败",e.toString());
        }
        return null;
    }


}
